﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class BATCHABAUFMData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = ""; 
        public int ThreadCount; 
        public BATCHABAUFMData()
            : base()
        {
            BuildTrisList();  
        }
        public BATCHABAUFMData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string BATCHNO;
            BATCHNO = dwABPOS["BATCHNO"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                /*
                 SELECT DISTINCT VVLIEFSCH,WBBDPART,RTRIM(WBBART)||'-'||RTRIM(WBBFARBE)||'-'||RTRIM(WBBVAR) AS ARTICLE,"
                    + "WBBBESCHICH,VVSTATUS, VVVERSART,VVSPED,VBEZ,VVAVISNR, COUNT(1) AS VVANZSTK,ROUND(SUM(0.001*WBBISTKLG)/0.9144,2) AS VVNETMTR,"
                    + "TRIM(HRNNAME) AS VKPNEUBEN,KDNAME1,VVFRDAT"
                    +" FROM VERSART,HRSTAMM,KDSTAMM,VVLIEF V JOIN VVTLIEF T ON V.VVLIEFSCH=T.VVTLIEFSCH"
                    + " JOIN WBBST ON VVLIEFSCH=WBBURLIEFSCH AND VVTTEILNR=WBBTP"
                    +" JOIN VKPOS ON VKPAUFKRS=VVTAUFKRS AND  VKPAUFLFD = VVTAUFLFD AND  VKPAUFPOS=VVTAUFPOS"
                    +" AND VKPSA<>'AJ' "
                    + " WHERE WBBWAKZ='F' AND VVFRDAT>='" + WBSRTDATE + "' AND VVFRDAT<='" + WBENTDATE + "'"
                    + " AND VKPKUNDNR=KDKUNDNR AND HRBENID=VKPNEUBEN AND VVVERSART=VVERSART AND VVSPED=VSPED"
                    + " GROUP BY VVLIEFSCH,WBBDPART,WBBART,WBBFARBE,WBBVAR,WBBBESCHICH,VVSTATUS, "
                    + " VVVERSART,VVSPED,VBEZ,VVAVISNR, VVANZSTK,HRNNAME,KDNAME1,VVFRDAT "
                    +" ORDER BY 1,2,3,4
                 */
                or1.CommandText = "SELECT  DISTINCT B.WBBDPART, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS WEAVINGORDER, "
                    + " D.ABAVKAUFKRS||'-'||TO_NUMBER(D.ABAVKAUFLFD)||'-'||TO_NUMBER(D.ABAVKAUFPOS) AS FINISHINGORDER "
                    + " FROM WBBEW A,WBBST B,ABAUFM C,ABAUFM D "
                    + " WHERE A.WBEWMANDANT = B.WBBMANDANT "
                    + " AND A.WBEWRWKRS = B.WBBRWKRS AND A.WBEWRWLFD = B.WBBRWLFD "
                    + " AND A.WBEWPOS = B.WBBPOS AND A.WBEWMANDANT = C.ABAMANDANT "
                    + " AND A.WBEWAUFKRS = C.ABAAUFKRS AND A.WBEWAUFLFD = C.ABAAUFLFD "
                    + " AND A.WBEWAUFPOS = C.ABAAUFPOS AND A.WBEWAUFTYP = C.ABAAUFTYP "
                    + " AND C.ABAAPOS = '0001' AND A.WBEWMANDANT = D.ABAMANDANT "
                    + " AND A.WBEWAUFKRS = D.ABAAUFKRS AND A.WBEWAUFLFD = D.ABAAUFLFD "
                    + " AND A.WBEWAUFPOS = D.ABAAUFPOS AND A.WBEWAUFTYP = D.ABAAUFTYP "
                    + " AND D.ABAAPOS > '0001' AND TRIM(B.WBBDPART) IS NOT NULL "
                    + " AND B.WBBDPART LIKE '" + BATCHNO + "%'"
                    + " AND A.WBEWBS = '13' AND trim(A.WBEWFC) = 'WB15' ";
                     ;        
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "BATCHABAUFM";
                    SetDefault(tableLocal);
                      
                    setLocal.AcceptChanges(); 
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        } 
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}